package service

import (
	"mall/admin/web/models"
	"log"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

type UserService struct{
     *xorm.Engine
}

func NewUserService(engine *xorm.Engine)*UserService{

	return &UserService{engine}
}

func (s *UserService)InsertUser(name string,age int32) error {

	log.Printf("name is %s,age is %d",name,age)

	return nil
}

//獲取所有的list
func (s *UserService)FindAll(pageSize,page int) (int64,[]models.Food,error) {
    foods:=make([]models.Food,0)
	err:=s.Limit(pageSize,page).Find(&foods)
	if err!=nil{
		return 0,nil,err
	}
	total,err:=s.Count(new(models.Food))
	if err!=nil{
		return 0,nil,err
	}
	return  total,foods,nil
}

//測試Join查詢方法
func (s *UserService)FindBusiness() ([]models.UserGroup,error) {

	users := make([]models.UserGroup, 0)
	s.Join("INNER", "business", "business.id = food.business_id").Find(&users)

	log.Println(users)

	return users,nil
}
